package com.ysm.smfu.map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MyLocationStyle;
import com.ysm.smfu.R;
import com.ysm.smfu.contans.SpKey;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BasicMapActivity extends AppCompatActivity implements AMap.OnMapLoadedListener, AMap.OnMarkerClickListener {

    @BindView(R.id.mapView)
    MapView mapView;
    @BindView(R.id.loc)
    ImageView loc;
    @BindView(R.id.loc2)
    ImageView loc2;
    @BindView(R.id.goMap_bg)
    AutoLinearLayout goMap_bg;
    private AMap aMap;
    private LocationTask mLocationTask;
    private PositionEntity positionEntity;
    private boolean singal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic_map);
        ButterKnife.bind(this);
        singal = getIntent().getBooleanExtra("singal", false);
        dealLocImgLoc();
        setMap(savedInstanceState);
        startLoc();
    }

    /**
     * 添加商家位置标记
     */
    private void addSignalMarker() {
//        FoodMapUtils.addSignalEmulateData(aMap,);
    }

    /**
     * 修正定位图标位置
     */
    private void dealLocImgLoc() {
        if (singal) {
            loc.setVisibility(View.GONE);
        } else {
            goMap_bg.setVisibility(View.GONE);
            loc2.setVisibility(View.GONE);
        }
    }

    private void setMap(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        aMap.setOnMapLoadedListener(this);
//        UiSettings uiSettings = aMap.getUiSettings();
//        uiSettings.setCompassEnabled(true);
        aMap.setInfoWindowAdapter(new CustomeInfoWindow(this));
        aMap.setOnMarkerClickListener(this);
    }

    private void moveToLevel(int level) {
        CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(level);
        aMap.animateCamera(cameraUpdate);
    }

    private void moveToPosition(LatLng latLonPoint) {
        CameraUpdate cameraUpdate = CameraUpdateFactory.changeLatLng(latLonPoint);
        aMap.animateCamera(cameraUpdate, 500, new AMap.CancelableCallback() {
            @Override
            public void onFinish() {
                moveToLevel(13);
            }

            @Override
            public void onCancel() {

            }
        });
    }

    double[] tianAnMen = {116.39629113, 39.91125549};
    @OnClick({R.id.back, R.id.loc, R.id.goOutSideMap})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.loc:
                if (positionEntity != null) {
                    moveToPosition(new LatLng(positionEntity.latitue, positionEntity.longitude));
                } else
                    startLoc();
                break;
            case R.id.goOutSideMap:
                if (positionEntity != null)//应该传递目的地，先传递定位位置
                    FoodMapUtils.startMap(this, new LatLng(tianAnMen[1], tianAnMen[0]), "板面");
                break;
        }
    }

    @Override
    public void onMapLoaded() {
        showLocIcon();
    }

    /**
     * 标志周边商家位置,不需要定位，暂时使用假数据
     */
    private void startLoc() {
        mLocationTask = LocationTask.getInstance();//获取定位单例
        mLocationTask.setOnLocationGetListener(new OnLocationGetListener() {
            @Override
            public void onLocationGet(PositionEntity entity) {
                positionEntity = entity;
                FoodMapUtils.removeMarkers();
                if (singal) {
                    FoodMapUtils.addEmulateData(aMap, new LatLng(entity.latitue, entity.longitude), null);
                } else {
                    moveToPosition(new LatLng(entity.latitue, entity.longitude));
                    FoodMapUtils.addEmulateData(aMap, new LatLng(entity.latitue, entity.longitude), new ArrayList<PositionEntity>());
                }
                mLocationTask.onDestroy();//销毁定位资源
            }

            @Override
            public void onRegecodeGet(PositionEntity entity) {

            }
        });//设置监听器
        mLocationTask.startSingleLocate();//开始定位
    }

    /**
     * 显示定位图标
     */
    private void showLocIcon() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myloc);
        MyLocationStyle myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        FoodMapUtils.removeMarkers();
        mapView.onDestroy();
        mLocationTask.onDestroy();
    }
}
